package com.czl.module_service.fragment.assetUse

import com.alibaba.android.arouter.facade.annotation.Route
import com.annimon.stream.Collectors
import com.annimon.stream.Stream
import com.blankj.utilcode.util.LogUtils
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.tengyun.SortBean
import com.czl.base.data.bean.tengyun.StorehouseBean
import com.czl.base.event.LiveBusCenter
import com.czl.base.util.DialogHelper
import com.czl.module_service.BR
import com.czl.module_service.R
import com.czl.module_service.adapter.asset.AssetStoreAllocationApplyAddAdapter
import com.czl.module_service.databinding.*
import com.czl.module_service.viewmodel.assetUse.AssetStoreAllocationApplyAddViewModel

@Route(path = AppConstants.Router.Service.F_SERVICE_ASSET_STORE_APPLY_ADD)
class AssetStoreAllocationApplyAddFragment : BaseFragment<FragmentAssetStoreAllocationApplyAddBinding, AssetStoreAllocationApplyAddViewModel>() {

    val list : MutableList<SortBean> = arrayListOf()

    var storeHouseList : List<StorehouseBean.StoreHouseListBean> = arrayListOf()
    var storeHouseInList : List<StorehouseBean.StoreHouseListBean> = arrayListOf()
    var storeHouseOutList : List<StorehouseBean.StoreHouseListBean> = arrayListOf()

    private val headerBinding: FragmentAssetStoreAllocationApplyAddHeaderBinding by lazy  {
        FragmentAssetStoreAllocationApplyAddHeaderBinding.inflate(layoutInflater).apply {

        }
    }

    private val footerBinding: FragmentAssetStoreAllocationApplyAddFooterBinding by lazy  {
        FragmentAssetStoreAllocationApplyAddFooterBinding.inflate(layoutInflater).apply {

        }
    }

    override fun initContentView(): Int {
        return R.layout.fragment_asset_store_allocation_apply_add
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun useBaseLayout(): Boolean {
        return false
    }

    private val mAdapter: AssetStoreAllocationApplyAddAdapter by lazy {
        AssetStoreAllocationApplyAddAdapter(this).apply {
            headerBinding.setVariable(initVariableId(), viewModel)
            addHeaderView(headerBinding.root)
            footerBinding.setVariable(initVariableId(), viewModel)
            addFooterView(footerBinding.root)
        }
    }

    override fun initData() {
        super.initData()
        initAdapter()
        viewModel.getSortList()
        viewModel.getStorehouseByUserId()
    }

    private fun initAdapter() {
        binding.recyclerView.run {
            adapter = mAdapter
        }
    }

    override fun initViewObservable() {
        LiveBusCenter.observeAddChoosedSortEvent(this, {
            LogUtils.e("----->" + it.sortBean)
            if (it.sortBean != null) {
                // 选中的物品添加
                val index = mAdapter.position - 1
                list[index] = it.sortBean!!
                viewModel.list = list
                mAdapter.setList(list)
            }
        })
        viewModel.uc.loadCompleteEvent.observe(this, { data ->
            list.add(data[0])
//            viewModel.list = list
            LogUtils.e("----->" + list.size)
            if (list.size > 1) {
                mAdapter.isDel = true
            }
            mAdapter.setList(list)
        })
        viewModel.uc.storeCompleteEvent.observe(this, { data ->
            storeHouseList = data
            storeHouseInList = data
            storeHouseOutList = data
        })

        // 选择调出仓库
        viewModel.uc.onOutStoreEvent.observe(this, {
            storeHouseOutList.let { itw ->
                DialogHelper.showBottomListDialog(
                    requireContext(),
                    Stream.of(itw).map { t -> t.storehouseName }.withoutNulls()
                        .collect(Collectors.toList()) as ArrayList<String>
                ) { position, text ->
                    headerBinding.tvOutStore.text = text
                    viewModel.storehouseOutId = itw[position].storehouseId
                    mAdapter.storehouseId = itw[position].storehouseId
                    storeHouseInList = storeHouseList.filter {
                        it.storehouseId != itw[position].storehouseId
                    }
                }
            }
        })

        // 选择调入仓库
        viewModel.uc.onInStoreEvent.observe(this, {
            storeHouseInList.let { itw ->
                DialogHelper.showBottomListDialog(
                    requireContext(),
                    Stream.of(itw).map { t -> t.storehouseName }.withoutNulls()
                        .collect(Collectors.toList()) as ArrayList<String>
                ) { position, text ->
                    headerBinding.tvInStore.text = text
                    viewModel.storehouseInId = itw[position].storehouseId
                    storeHouseOutList = storeHouseList.filter {
                        it.storehouseId != itw[position].storehouseId
                    }
                }
            }
        })

        viewModel.uc.addCompleteEvent.observe(this, { data ->
            viewModel.applyComment.set("")
            viewModel.storehouseOutId = null
            viewModel.storehouseInId = null
            headerBinding.tvInStore.text = ""
            headerBinding.tvOutStore.text = ""
            val list1 : MutableList<SortBean> = arrayListOf()
            list1.add(SortBean())
            mAdapter.setList(list1)
        })

    }

    fun deleteList(sortBean: SortBean) {
        list.removeAt(sortBean.position!! - 1)
//        viewModel.list.removeAt(sortBean.position!!)
        if (list.size == 1) {
            mAdapter.isDel = false
        }
        mAdapter.setList(list)
    }
}